//
//  CETestClient.m
//  CELog_Example
//
//  Created by maoerduo on 2021/2/26.
//  Copyright © 2021 maoerduo. All rights reserved.
//

#import "CETestClient.h"
#import "CETestCaseCache.h"

@interface CETestClient()

@property (nonatomic, strong) NSMutableDictionary<NSString *, id<CETestCase>> * tests;

@property (nonatomic, strong) id<CETestCaseRequestParameter> currentParameter;

@property (nonatomic, strong) CETestCaseCache * testCaseCache;

@end

@implementation CETestClient

+ (instancetype)shared {
    
    static CETestClient * client = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        client = [[self alloc] init];
    });
    
    return client;
    
}

- (instancetype)init {
    self = [super init];
    if (self) {
        self.tests = [NSMutableDictionary dictionary];
    }
    return self;
}

- (void)setupWithConfig:(CETestClientConfig *)config parameter:(nonnull id<CETestCaseRequestParameter>)parameter {
    self->_config = config;
    self.currentParameter = parameter;
}

- (void)registTest:(id<CETestCase>)test {
    
    self.tests[test.identifier] = test;
    
}

- (void)removeTest:(NSString *)testID {
    
    [self.tests removeObjectForKey:testID];
    
}

- (void)synchronizeTests {
    
    //每次读取的时候都从缓存里读取。然后请求服务器更新缓存。
    [self.tests enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, id<CETestCase>  _Nonnull obj, BOOL * _Nonnull stop) {
        [obj updateConifg:[[CETestCaseCache shared] loadForTestCase:obj]];
    }];
     
    //网络请求最新的策略
    
}

- (id<CETestCase> _Nullable)getTest:(NSString *)testID {
    return self.tests[testID];
}

@end
